<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/8/30
 * Time: 14:52
 */

namespace Admin\Logic;


use Common\Utils\Constants;
use Think\Exception;
use Think\Log;

class RaceLogic
{
    /**
     * 根据时间判断当前赛事状态
     * @param $race array 赛事
     * @return int 状态
     */
    public function getStatus($race){
        $now = time();
//        $race['status'] = 0;
        if($race['status'] == Constants::RACE_STATUS_GAME_END){
            return $race['status'];
        }else {
            if($race['game_start_time'] > $now){//赛事未开始
                if($race['apply_end_time'] > $now){//赛事报名未结束
                    if($race['apply_start_time'] > $now){//报名未开始
                        return Constants::RACE_STATUS_APPLY_WAIT;
                    }else{//报名中
                        return Constants::RACE_STATUS_APPLY_START;
                    }
                }else{//报名结束
                    return Constants::RACE_STATUS_GAME_START;
                }
            }else{
                //赛事进行中
                return Constants::RACE_STATUS_GAME_START;
            }
        }
    }

    /**
     * 更新赛事状态
     * @param $list array 赛事列表
     */
    public function updateSatus(&$list){
        $model = M();
        $model->startTrans();
        try {
            foreach ($list as $key => $value) {
                $status = $this->getStatus($value);
                if ($value['status'] != $status) {
                    $list[$key]['status'] = $status;
                    //修改状态
                    $model->table('t_race')->where(array('id' => $value['id']))->setField('status', $status);
                };
            }
            $model->commit();
        }catch (Exception $e){
            Log::write($e->getMessage());
            $model->rollback();
        }
    }

    /**
     * 指定赛事获胜者
     * @param $user_id int 用户id
     * @param $race_id int 赛事id
     * @param $result string 判定结果
     * @return int 0失败 1成功 2该用户没有报名该赛事 3该赛事不能判定
     */
    public function judge($user_id,$race_id,$result){
        $is_involved = D('ApplyLog')->where(array('race_id'=>$race_id,'user_id'=>$user_id))->count();
        if($is_involved == 0){
            return 2;//该用户没有报名该赛事
        }
        $race = D('Race')->where(array('id'=>$race_id))->find();
        $status = $this->getStatus($race);
        if($status != Constants::RACE_STATUS_GAME_START){
            return 3;
        }
        $model = M();
        $model->startTrans();
        try{
            //获胜者奖励钻石
            $this->reward($user_id,$race,$model);
            //修改赛事状态
            $model->table('t_race')->where(array('id'=>$race_id))->setField('result',$result);
            $model->table('t_race')->where(array('id'=>$race_id))->setField('status',Constants::RACE_STATUS_GAME_END);
            $model->table('t_race')->where(array('id'=>$race_id))->setField('winner',$user_id);
            $model->commit();
            return 1;
        }catch (Exception $e){
            Log::write($e->getMessage());
            $model->rollback();
            return 0;
        }
    }

    //获胜者奖励钻石
    private function reward($user_id,$race,$model){

        $money = $race['reward_integral'];
        $model->table('t_user')->where(array('id'=>$user_id))->setInc('integral',$money);
        $currentMoney =  $model->table('t_user')->where(array('id'=>$user_id))->getField('integral');
        //存储资金流动
        $remark = '赢得比赛('.$race['title'].')';
        $moneyLogLogic = new MoneyLogLogic();
        $moneyLogLogic->saveMoneyLog($money,
            $currentMoney,
            Constants::MONEY_SOURCE_WIN,
            Constants::PUT_TYPE_IN,
            $remark, $user_id,Constants::MONEY_TYPE_INTEGRAL, $model);
    }
}